package com.zho.dao.impl;

import java.util.List;

import com.zho.dao.ICgsUidDAO;
import com.zho.domain.bean.CgsUid;
import com.zho.util.Logit;

import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * CgsUid entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.zho.domain.bean.CgsUid
 * @author MyEclipse Persistence Tools
 */

public class CgsUidDAO extends BaseHelperDAO implements ICgsUidDAO {
	private static final Logit log = Logit
	.getInstance(CgsUidDAO.class);
	// property constants
	public static final String COUNT = "count";
	public static final String ACTIVE_FLG = "activeFlg";

	protected void initDao() {
		// do nothing
	}

	public void save(CgsUid transientInstance) {
		log.debug("saving CgsUid instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CgsUid persistentInstance) {
		log.debug("deleting CgsUid instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CgsUid findById(String id) {
		log.debug("getting CgsUid instance with id: " + id);
		try {
			CgsUid instance = (CgsUid) getHibernateTemplate().get(
					"com.zho.domain.bean.CgsUid", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CgsUid instance) {
		log.debug("finding CgsUid instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CgsUid instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CgsUid as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCount(Object count) {
		return findByProperty(COUNT, count);
	}

	public List findByActiveFlg(Object activeFlg) {
		return findByProperty(ACTIVE_FLG, activeFlg);
	}

	public List findAll() {
		log.debug("finding all CgsUid instances");
		try {
			String queryString = "from CgsUid";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CgsUid merge(CgsUid detachedInstance) {
		log.debug("merging CgsUid instance");
		try {
			CgsUid result = (CgsUid) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CgsUid instance) {
		log.debug("attaching dirty CgsUid instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CgsUid instance) {
		log.debug("attaching clean CgsUid instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	//synchronized
	public String genSequenceId(String prefix, String code) throws Exception {

		String idStr = "";
		try {
			CgsUid cgsUid = (CgsUid)getHibernateTemplate().get("com.zho.domain.bean.CgsUid", code, LockMode.UPGRADE);
			Long id = cgsUid.getCount();
			id++;
			idStr = prefix + code + id; 
			cgsUid.setCount(id);
			getHibernateTemplate().saveOrUpdate(cgsUid);
			
		} catch (Exception e) {
			// TODO: handle exception
			log.error("genId failed", e);
			throw e;
		}
		return idStr;
	}
}